package com.irm.web.system.management.action;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.irm.model.system.AuditLog;
import com.irm.model.system.ServiceModuleAccessCount;
import com.irm.model.system.User;
import com.irm.model.system.criteria.AuditLogCriteria;
import com.irm.system.access.namespace.service.AuditLogAccessService;
import com.irm.system.access.namespace.service.ServiceModuleAccessCountAccessService;
import com.irm.system.access.namespace.service.UserAccessService;
import com.irm.type.AuditLogSeverity;
import com.irm.web.system.common.action.AbstractAction;
import com.platform.model.Model;
import common.util.ChineseSpellingUtil;
import common.util.Detect;
import common.vo.Ordering;
import common.vo.Pagination;

public class SystemAuditLogManagementAction extends AbstractAction {

	/**
	 * 
	 */
	private static final long serialVersionUID = -5577735256864740468L;

	//
	private AuditLogSeverity[] auditLogSeveritys;

	private AuditLogCriteria auditLogCriteria;

	private String auditLogSeverity;

	private List<User> users;
	//
	private long auditLogId;

	private AuditLog auditLog;

	private Pagination<AuditLog> pagination;

	private List<Map<String, Object>> operatorHits;

	private List<Map<String, Object>> moduleHits;
	
	private ServiceModuleAccessCount serviceModuleAccessCount;
	
	//
	private AuditLogAccessService auditLogAccessService;

	private ServiceModuleAccessCountAccessService serviceModuleAccessCountAccessService;
	
	private UserAccessService userAccessService;

	public String execute() throws Exception {
		auditLogSeveritys = AuditLogSeverity.values();
		users = userAccessService.findAll(new Ordering(Model.Column_NAME, Ordering.DESC), this.getUserContext());
		return SUCCESS;
	}

	public String findAuditLogs() throws Exception {
		Ordering ordering = new Ordering(AuditLog.Column_STARTTIME, Ordering.DESC);

		if (null != auditLogSeverity && !"".equals(auditLogSeverity)) {
			if (null == auditLogCriteria) {
				auditLogCriteria = new AuditLogCriteria();

			}
			auditLogCriteria.setAuditLogSeverity(AuditLogSeverity.get(auditLogSeverity));
		}

		pagination = auditLogAccessService.findPagination(auditLogCriteria, ordering, pagination, this.getUserContext());
		return "findAuditLogs";
	}

	public String capitalize(String string) throws Exception {
		string = ChineseSpellingUtil.getFullSpell(string);
		if (Detect.notEmpty(string)) {
			return string.substring(0, 1).toUpperCase();
		}
		return null;
	}

	public String findAuditLogById() throws Exception {
		auditLog = auditLogAccessService.findById(auditLogId, this.getUserContext());
		return "findAuditLogById";
	}

	public String findOperatorHits() throws Exception {
		operatorHits = auditLogAccessService.findOperatorHits(this.getUserContext());
		if (Detect.notEmpty(operatorHits)) {
			int count = 0;
			for (Map<String, Object> operatorHit : operatorHits) {
				count += Integer.parseInt(String.valueOf(operatorHit.get("TOTAL_")));
			}
			for (Map<String, Object> operatorHit : operatorHits) {
				operatorHit.put("TOTAL_PRECENT", Integer.parseInt(String.valueOf(operatorHit.get("TOTAL_"))) * 100.1 / count);
			}
		}

		return "findOperatorHits";
	}

	@SuppressWarnings({ "rawtypes", "unchecked" })
	public String findModuleHits() throws Exception {
		String[] strArray = {"Topology","WebGis","Fulfillment","CutOver","Statistics","Inventory"};
		int arrayLength = strArray.length;
		Map map = null;
		moduleHits = new ArrayList<Map<String, Object>>();
		for(int i=0; i<arrayLength; i++){
			serviceModuleAccessCount = serviceModuleAccessCountAccessService.findByCode(strArray[i], this.getUserContext());
			if(serviceModuleAccessCount != null){
				map = new HashMap();
				map.put("ServiceModuleCode_", strArray[i]);
				map.put("COUNT_", serviceModuleAccessCount.getAccessCount());
				moduleHits.add(map);
			}
		}
//		moduleHits = auditLogAccessService.findModuleHits(this.getUserContext());
		return "findModuleHits";
	}

	/**
	 * @return the moduleHits
	 */
	public List<Map<String, Object>> getModuleHits() {
		return moduleHits;
	}

	/**
	 * @param moduleHits
	 *            the moduleHits to set
	 */
	public void setModuleHits(List<Map<String, Object>> moduleHits) {
		this.moduleHits = moduleHits;
	}

	/**
	 * @return the users
	 */
	public List<User> getUsers() {
		return users;
	}

	/**
	 * @param users
	 *            the users to set
	 */
	public void setUsers(List<User> users) {
		this.users = users;
	}

	public ServiceModuleAccessCount getServiceModuleAccessCount() {
		return serviceModuleAccessCount;
	}
	
	public void setServiceModuleAccessCount(
			ServiceModuleAccessCount serviceModuleAccessCount) {
		this.serviceModuleAccessCount = serviceModuleAccessCount;
	}

	/**
	 * @return the userAccessService
	 */
	public UserAccessService getUserAccessService() {
		return userAccessService;
	}

	/**
	 * @param userAccessService
	 *            the userAccessService to set
	 */
	public void setUserAccessService(UserAccessService userAccessService) {
		this.userAccessService = userAccessService;
	}

	/**
	 * @return the auditLogSeveritys
	 */
	public AuditLogSeverity[] getAuditLogSeveritys() {
		return auditLogSeveritys;
	}

	/**
	 * @param auditLogSeveritys
	 *            the auditLogSeveritys to set
	 */
	public void setAuditLogSeveritys(AuditLogSeverity[] auditLogSeveritys) {
		this.auditLogSeveritys = auditLogSeveritys;
	}

	/**
	 * @return the pagination
	 */
	public Pagination<AuditLog> getPagination() {
		return pagination;
	}

	/**
	 * @param pagination
	 *            the pagination to set
	 */
	public void setPagination(Pagination<AuditLog> pagination) {
		this.pagination = pagination;
	}

	/**
	 * @return the auditLogAccessService
	 */
	public AuditLogAccessService getAuditLogAccessService() {
		return auditLogAccessService;
	}

	/**
	 * @param auditLogAccessService
	 *            the auditLogAccessService to set
	 */
	public void setAuditLogAccessService(AuditLogAccessService auditLogAccessService) {
		this.auditLogAccessService = auditLogAccessService;
	}
	
	public ServiceModuleAccessCountAccessService getServiceModuleAccessCountAccessService() {
		return serviceModuleAccessCountAccessService;
	}

	public void setServiceModuleAccessCountAccessService(
			ServiceModuleAccessCountAccessService serviceModuleAccessCountAccessService) {
		this.serviceModuleAccessCountAccessService = serviceModuleAccessCountAccessService;
	}

	/**
	 * @return the auditLogId
	 */
	public long getAuditLogId() {
		return auditLogId;
	}

	/**
	 * @param auditLogId
	 *            the auditLogId to set
	 */
	public void setAuditLogId(long auditLogId) {
		this.auditLogId = auditLogId;
	}

	/**
	 * @return the auditLog
	 */
	public AuditLog getAuditLog() {
		return auditLog;
	}

	/**
	 * @return the auditLogCriteria
	 */
	public AuditLogCriteria getAuditLogCriteria() {
		return auditLogCriteria;
	}

	/**
	 * @param auditLogCriteria
	 *            the auditLogCriteria to set
	 */
	public void setAuditLogCriteria(AuditLogCriteria auditLogCriteria) {
		this.auditLogCriteria = auditLogCriteria;
	}

	/**
	 * @param auditLog
	 *            the auditLog to set
	 */
	public void setAuditLog(AuditLog auditLog) {
		this.auditLog = auditLog;
	}

	/**
	 * @return the auditLogSeverity
	 */
	public String getAuditLogSeverity() {
		return auditLogSeverity;
	}

	/**
	 * @param auditLogSeverity
	 *            the auditLogSeverity to set
	 */
	public void setAuditLogSeverity(String auditLogSeverity) {
		this.auditLogSeverity = auditLogSeverity;
	}

	/**
	 * @return the operatorHits
	 */
	public List<Map<String, Object>> getOperatorHits() {
		return operatorHits;
	}

	/**
	 * @param operatorHits
	 *            the operatorHits to set
	 */
	public void setOperatorHits(List<Map<String, Object>> operatorHits) {
		this.operatorHits = operatorHits;
	}

}
